package org.taobao.ibatiside.ui.adapter;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.model.WorkbenchAdapter;
import org.taobao.ibatiside.core.IbatisConstant;
import org.taobao.ibatiside.core.filemodel.FileProber;
import org.taobao.ibatiside.core.filemodel.IbatisConfigFile;
import org.taobao.ibatiside.core.filemodel.IbatisProject;
import org.taobao.ibatiside.ui.Activator;

public class ProjectAdapter extends WorkbenchAdapter {
	private final IbatisProject project;
	public ProjectAdapter(IbatisProject project) {
		super();
		this.project = project;
	}
	@Override
	public Object[] getChildren(Object object) {
		final IJavaProject java_proj = JavaCore.create(project.getProject());
		final List<IbatisConfigFile> configs = new ArrayList<IbatisConfigFile>();
		try {
			PlatformUI.getWorkbench().getProgressService().run(false,false,new IRunnableWithProgress() {
				
				@Override
				public void run(IProgressMonitor monitor) throws InvocationTargetException,
						InterruptedException {
					try{
					if (!java_proj.getProject().isAccessible()) return;
					IPackageFragmentRoot[] roots = java_proj.getPackageFragmentRoots();
					for (IPackageFragmentRoot r : roots){
						if (r.getKind() == IPackageFragmentRoot.K_SOURCE){
							monitor.subTask("scanning source folder "+r.getElementName());
							searchSrcFolder((IFolder) r.getResource(),monitor);
						}
					}
					}catch(Exception e3){
						throw new InvocationTargetException(e3);
					}
				}

				private void searchSrcFolder(IFolder folder,
						IProgressMonitor monitor) throws CoreException {
					for (IResource r : folder.members()){
						if (r instanceof IFile){
							IFile file = (IFile) r;
							if ("xml".equalsIgnoreCase(file.getFileExtension())){
								monitor.subTask("determine file "+file.getName());
								if ((project.getVersion() == IbatisConstant.VERSION_TWO && FileProber.isIbatis2Config(file))
										|| project.getVersion() == IbatisConstant.VERSION_MYBATIS && FileProber.isMyIbatisConfig(file)){
									IbatisConfigFile config = new IbatisConfigFile(file, project);
									configs.add(config);
								}
							}
						}else if (r instanceof IFolder){
							monitor.subTask("scanning sub folder "+r.getName());
							searchSrcFolder((IFolder)r,monitor);
						}
					}
				}
			}
			);
		} catch (Exception e){
			e.printStackTrace();
		}
		
		return configs.toArray();
	}
	@Override
	public String getLabel(Object object) {
		return project.getProject().getName();
	}
	@Override
	public ImageDescriptor getImageDescriptor(Object object) {
		if (project.getVersion() == IbatisConstant.VERSION_TWO){
			return Activator.getIcon("eview16/batisv2.png");
		}else if (project.getVersion() == IbatisConstant.VERSION_MYBATIS){
			return Activator.getIcon("eview16/batisv3.png");
		}
		return null;
	}
	@Override
	public Object getParent(Object object) {
		return project.getProject().getParent();
	}
}
